<template>
  <div>
    <div class="toggle-button" @click="toggleCollapse">|||</div>
    <!-- 侧边栏菜单 -->
    <el-menu
      router
      :collapse="collapse"
      :collapse-transition="false"
      :default-active="route"
      text-color="#fff"
      unique-opened
      background-color="#333744"
      active-text-color="#409eff"
      class="el-menu-vertical-demo">
      <!-- 一级菜单 -->
      <el-submenu v-for="item in menuList" :key="item.id" :index="'/'+item.path" >
        <!-- 一级菜单模板区域 -->
        <template slot="title">
          <!-- 图标 -->
          <i :class="iconObj[item.id]"></i>
          <!-- 文本 -->
          <span>{{ item.authName }}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/'+subItem.path"
        @click="clickMenu(subItem.path)"
        >
          <!-- 二级菜单模板区域 -->
          <template slot="title">
            <!-- 图标 -->
            <i class="el-icon-menu"></i>
            <!-- 文本 -->
            <span>{{ subItem.authName }}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
  </div>
</template>
<script>
export default{
  name:"men-us",
  data() {
    return {
      collapse:false, // 是否折叠 ，默认不折叠
      menuList:[],   // 左侧菜单
      // 一级菜单图标
      iconObj:{
        '125':'el-icon-user-solid',
        '103':'el-icon-s-help',
        '101':'el-icon-s-shop',
        '102':'el-icon-s-order',
        '145':'el-icon-s-data'
      }
    };
  },
  computed:{
    route(){
      return this.$route.path.indexOf("goods") !== -1 ? '/goods':this.$route.path
        && this.$route.path.indexOf("orders") !== -1 ? '/orders':this.$route.path; 
    }
  },
  methods:{
    /**
     * @获取菜单
     */
    async getmenu(){
      // let {data:{data,meta:{status,msg}}} = await getMenu();
      const {data:{meta:{status,msg},data}} = await this.$http.get('/menus');
      if(status !== 200) return this.$message.error(msg);
      this.menuList = data;
    },
    /**
     * @点击按钮切换菜单折叠与展开
     */
    toggleCollapse(){
      this.collapse = !this.collapse
      this.$emit('collapse',this.collapse);
    },
    clickMenu(item){
      this.$store.commit('selectMenu', item)
    }
  },
  created(){    
    this.getmenu();
  }
}
</script>
<style lang="less" scoped>
  .el-menu{
    border-right: 0;
  }
  // 收缩按钮
  .toggle-button{
    color: #fff;
    font-size: 10px;
    cursor: pointer;
    text-align: center;
    line-height: 24px;
    letter-spacing: 0.2em;
    background-color: #4a506a;
  }
</style>